package nl.vu.few.ibivu.biomodels.shared;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Embedded;
import javax.persistence.Id;
import javax.persistence.PrePersist;
import javax.persistence.Transient;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Indexed;
import com.googlecode.objectify.annotation.Unindexed;
import com.googlecode.objectify.condition.IfFalse;

public class Model implements Serializable {
	private static final long serialVersionUID = 3691715155946634543L;
	private @Id Long id;
	private @Indexed String name;
	private @Indexed Key<UserProfile> owner;
	private @Unindexed(IfFalse.class) boolean ispublic;
	private @Embedded PetrifierModelData data;
	private @Transient UserProfile ownerInstance;
	private @Unindexed String thumbnail;
	private String description;
	private Date creationTime;
	private Date modifiedTime;

	public Model() {
		super();
		data = new PetrifierModelData();
	}
	
	public Model(UserProfile owner) {
		this();
		setOwner(owner);
	}

	@SuppressWarnings("unused")
	@PrePersist
	private void updateDates() {
		Date now = new Date();
		if (creationTime == null)
			creationTime = now;
		modifiedTime = now;
	}
	
	public Date getCreationTime() {
		return creationTime;
	}

	public Date getModifiedTime() {
		return modifiedTime;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public UserProfile getOwner() {
		return ownerInstance;
	}
	
	public Key<UserProfile> getOwnerKey() {
		return owner;
	}

	public void setOwner(UserProfile owner) {
		this.ownerInstance = owner;
		this.owner = (owner == null) ? null : new Key<UserProfile>(UserProfile.class, owner.getId());
	}

	public PetrifierModelData getData() {
		return data;
	}

	public void setData(PetrifierModelData data) {
		this.data = data;
	}

	public Boolean isPublic() {
		return ispublic;
	}
	
	public void setPublic(Boolean ispublic) {
		this.ispublic = ispublic;
	}
	
	public Long getId() {
		return id;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}
	
	public Model copyData(Model model) {
		// FIXME: implement deep clone instead of this shallow copy
		this.data = model.data;
		this.ispublic = model.ispublic;
		this.name = model.name;
		this.owner = model.owner;
		this.ownerInstance = model.ownerInstance;
		this.description = model.description;
		this.thumbnail = model.thumbnail;
		return this;
	}

	public String getThumbnail() {
		return thumbnail;
	}

	public void setThumbnail(String thumbnail) {
		this.thumbnail = thumbnail;
	}
}
